package com.originskyseed.platform.job.jobhandler.count;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.job.biz.model.ReturnT;
import com.originskyseed.platform.core.job.handler.IJobHandler;
import com.originskyseed.platform.core.job.handler.annotation.JobHandler;
import com.originskyseed.platform.core.job.log.JobLogger;
import com.originskyseed.platform.count.service.IUserAchievementService;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 统计用户业绩信息功能
 * 
 * @Author: twh @date: 2021/6/3
 */

@Component
@JobHandler(value = "UserAchievementHandler")
public class UserAchievementHandler extends IJobHandler {

	@DubboReference
	private IUserInfoService userInfoService;

	@DubboReference
	private IUserAchievementService userAchievementService;

	@Override
	public ReturnT<String> execute(String tenantCode) throws Exception {
		try {
			// 获取开始统计的时间
			List<String> params = Arrays.asList(tenantCode.split(","));

			// 如果有传参则根据传递的参数查询，没有则根据当前日期查询
			String time = null;
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
			if (params.size() > 1) {
				time = params.get(1);
				tenantCode = params.get(0);
			} else {
				time = sim.format(new Date());
			}

			this.userAchievementService.insertsUserBonusByTime(time, tenantCode);

		} catch (Exception e) {
			JobLogger.log("统计季度年度奖金数据：" +getExceptionDetail(e));
			return FAIL;
		}
		return SUCCESS;
	}

	/**
	 * 获取当前季度
	 *
	 */
	public static int getQuarter() {
		Calendar c = Calendar.getInstance();
		int month = c.get(c.MONTH) + 1;
		int quarter = 0;
		if (month >= 1 && month <= 3) {
			quarter = 1;
		} else if (month >= 4 && month <= 6) {
			quarter = 2;
		} else if (month >= 7 && month <= 9) {
			quarter = 3;
		} else {
			quarter = 4;
		}
		return quarter;
	}

	/**
	 * 获取某季度的第一天和最后一天
	 * 
	 * @param num 第几季度
	 */
	public static String[] getCurrQuarter(int num) {
		String[] s = new String[2];
		String str = "";
		// 设置本年的季
		Calendar quarterCalendar = null;
		switch (num) {
		case 1: // 本年到现在经过了一个季度，在加上前4个季度
			quarterCalendar = Calendar.getInstance();
			quarterCalendar.set(Calendar.MONTH, 3);
			quarterCalendar.set(Calendar.DATE, 1);
			quarterCalendar.add(Calendar.DATE, -1);
			str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
			s[0] = str.substring(0, str.length() - 5) + "01-01";
			s[1] = str;
			break;
		case 2: // 本年到现在经过了二个季度，在加上前三个季度
			quarterCalendar = Calendar.getInstance();
			quarterCalendar.set(Calendar.MONTH, 6);
			quarterCalendar.set(Calendar.DATE, 1);
			quarterCalendar.add(Calendar.DATE, -1);
			str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
			s[0] = str.substring(0, str.length() - 5) + "04-01";
			s[1] = str;
			break;
		case 3:// 本年到现在经过了三个季度，在加上前二个季度
			quarterCalendar = Calendar.getInstance();
			quarterCalendar.set(Calendar.MONTH, 9);
			quarterCalendar.set(Calendar.DATE, 1);
			quarterCalendar.add(Calendar.DATE, -1);
			str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
			s[0] = str.substring(0, str.length() - 5) + "07-01";
			s[1] = str;
			break;
		case 4:// 本年到现在经过了四个季度，在加上前一个季度
			quarterCalendar = Calendar.getInstance();
			str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd");
			s[0] = str.substring(0, str.length() - 5) + "10-01";
			s[1] = str.substring(0, str.length() - 5) + "12-31";
			break;
		}
		return s;
	}

	/**
	 * 获取当前年的第一天
	 * 
	 * @return
	 */
	public static String getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
		Date time = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(time);
	}

	/**
	 * 获取当前年的最后一天
	 * 
	 * @return
	 */
	public static String getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date time = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(time);
	}

	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 *
	 * @param ex
	 * @return
	 */
	public static String getExceptionDetail(Exception ex) {
		String ret = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PrintStream pout = new PrintStream(out);
		try {
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
		} catch (Exception e) {
		} finally {
			try {
				pout.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

}
